
$.fn.validate = function () {
    
    if ($(this).data('error')){
        if ((this).parent().find('.errors').size() > 0){
            return;
        }
        $(this).addClass('error');
        var $_ul = $("<ul class='errors'></ul>");
        var $_li = $("<li></li>").text($(this).data("message"));
        $_ul.append($_li);
        $(this).parent().append($_ul);
        return;
    }
    $(this).removeClass('error');
    $(this).parent().find('.errors').remove();
}
// validate password -----------------------------------------------------------
$.fn.validatePassword = function(){
    $(this).data("message", "Password must contain at least 6 characters incluing letters and numbers.");
    $(this).change(function(){
        var pattern = new RegExp(/^([a-zA-Z]+[0-9]+)|([[0-9]+[a-zA-Z]+)|([[0-9]+[a-zA-Z]+[0-9]+)|([[a-zA-Z]+[0-9]+[a-zA-Z]+)$/);
        $(this).data('error', $(this).val().length < 6 || !pattern.test($(this).val()));
        $(this).validate();
    });
}
// confirm password --------------------------------------------------------------
$.fn.validateConfirmPassword = function ($identicalElement) {
    $(this).data("message", "Password do not match.");
    $(this).keyup(function(){
        if($identicalElement.val().indexOf($(this).val()) != 0) {
            $identicalElement.addClass('error');
            $(this).data('error', 'true');
        }else{
            $identicalElement.removeClass('error');
            $(this).data('error', false)
        }
        $(this).validate();
    });
    
    $(this).change(function(){
        if($(this).val() != $identicalElement.val()){
            $(this).data('error', 'true');
            $identicalElement.addClass('error');
            $(this).validate();
            $identicalElement.removeClass('field-success');
            $(this).removeClass('field-success');
        } else {
            if ($identicalElement.data('error')){
                return;
            }
            $identicalElement.removeClass('error');
            $identicalElement.addClass('field-success');
            $(this).addClass('field-success');
        }
    });
}
// validate e-mail --------------------------------------------------------------
$.fn.validateEmail = function(){
    $(this).data("message", "The e-mail address is incorrect");
    $(this).change(function(){
        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        var result = pattern.test($(this).val());
        $(this).data('error', !result);
        $(this).validate();
        
        if (!$(this).data('error')){
            // ajax ...
            var $_this = $(this);
            $.post($(this).attr('rel'), {
                value   : $(this).val(), 
                'format':'json'
            }, function(data){
                if (data.errors){
                    $_this.data("message", data.errors);
                    $_this.data('error', true);
                    $_this.validate();
                }else {
                    $_this.addClass('field-success');
                }
            })
        }
    });
}
